<?php

require_once 'quickstart-loading-re.php';


class re_point extends re_Scalar
{

	public function _declarePossRep(){
		
		return array(
		
			'cartesian'=>array(
				'x'=>'double',
				'y'=>'double'
			),
			
			'polar'=>array(
				'r'=>'double',
				'theta'=>'double'
			)			
		);
	}
	
	protected function _selectorCartesian($x, $y){
		
		$this->_setThe("x", $x);
		$this->_setThe("y", $y);			
	}
	
	protected function _selectorPolar($r, $theta){
	
		$this->_selectorCartesian(
			$r * (cos($theta))
		   ,$r * (sin($theta))
		);
	}
	
	public function theX(){
		return $this->_getThe("x");
	}
	
	public function theY(){
	
		return $this->_getThe("y");
	}
	
	
	public function theR(){
	
		return sqrt(pow($this->theX(), 2) + (pow($this->theY(), 2)));
	}
	
	public function theTheta(){

		return arctan($this->theY() / $this->theX());
	}

	public function toHTML($scaleX = 1, $scaleY = 1){
	
		//scaling...implementation related.
		$y        = $this->theY();
		$yBase = (300 - ($y * $scaleY));
		
		$x        = $this->theX();
		$xBase = (600 + ($x * $scaleX));
		
		return sprintf('<div style="position:absolute;margin:%dpx 0px 0px %dpx;border:1px solid black;width:2px;height:2px;background-color:black;"></div>', $yBase, $xBase);
	}
	
	//implementation related operators
	//standard selector
	static public function _ss($rep){
		
		$rep = (object)re_List::value((array)json_decode($rep))->rep();
		
		return re::select(
			__CLASS__
		  ,array(
				//necessary to cast the components, because sometimes, driver storage messes
				//the original type.
				'x'=>$rep->X
			   ,'y'=>$rep->Y
		   )
		  ,'cartesian'
		);
	}
	
	//standard representations
	public function _sr( $recurse = FALSE ){
	
		re_CodeFile::load('re_List');
		return re_List::value(array(
			'x'=>$this->theX()
		   ,'y'=>$this->theY()
		))->rep();
	}

	//standard storage representation
	public function _ssr(){
		
		return json_encode($this->_sr());
	}
}


foreach (range(-250, 250) as $x) {
	$p = re::select('re_point', array(0, $x), 'cartesian');
	echo $p->toHTML();
	$p = re::select('re_point', array($x, 0), 'cartesian');
	echo $p->toHTML();
}

$s = -15;
$e = 15;

//upwards, downwards
foreach (range($s, $e) as $x) {
$p = re::select('re_point', array($x, pow($x , 2)), 'cartesian');
echo $p->toHTML(10);
$p = re::select('re_point', array($x, -pow($x , 2)), 'cartesian');
echo $p->toHTML(10);
}

$s = -500;
$e = 500;
//sidewards
foreach (range($s, $e, 3) as $x) {
$p = re::select('re_point', array($x, pow(abs($x), .5)), 'cartesian');
echo $p->toHTML(1, 2);
$p = re::select('re_point', array($x, -pow(abs($x), .5)), 'cartesian');
echo $p->toHTML(1, 2);
}